-------------------------------------------------------------------------------
---! 对外接口
-------------------------------------------------------------------------------
ARTIFACT_D = {}

function ARTIFACT_D:is_artifact_opend(player)
    local common_config = IMPORT_D:query("swcommon")
    if not common_config then
        return false
    end

    ---! 翅膀功能对应解锁关卡
    if player:get_stage_level() < common_config.swUnlock then
        return false
    end

    return true
end

---! 查找神器对象
function ARTIFACT_D:find_artifact(swid, swlev)
    local configs = IMPORT_D:query("sw")
    if not configs then
        return
    end

    local config = configs[swid]
    if not config then
        return
    end

    local artifact = config[swlev or 1]
    if not artifact then
        return
    end

    return artifact
end

---! 改变神器
function ARTIFACT_D:change_artifact(player, swid)
    local swlev = player:get_artifact_level(swid)
    if not swlev then
        ---! 尚未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CArtifactChange", result)
        return
    end

    ---! 修改当前玩家神器
    player:set_use_artifact(swid)

    local result = {}
    result.error = 0
    result.id = swid
    player:send_packet("MSGS2CArtifactChange", result)
end

---! 提升神器
function ARTIFACT_D:improve_artifact_level(player, swid)
    local swlev = player:get_artifact_level(swid)
    if not swlev then
        ---! 尚未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CArtifactLevelup", result)
        return
    end

    local artifact = ARTIFACT_D:find_artifact(swid, swlev)
    if not artifact then
        ---! 神器配置不存在
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CArtifactLevelup", result)
        return
    end

    local next_artifact = ARTIFACT_D:find_artifact(swid, artifact.level + 1)
    if not next_artifact then
        ---! 当前神器已满级
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CArtifactLevelup", result)
        return
    end

    local sw = player:get_artifact(swid)
    if not sw then
        ---! 当前神器不存在
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CArtifactLevelup", result)
        return
    end

    local stone = math.floor(artifact.useDew * (artifact.useDewUp ^ sw.step))
    if player:get_currency(GamePropIds.kGamePropIdsStoneIcon) < stone then
        ---! 所需神石不足
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CArtifactLevelup", result)
        return
    end

    ---! 扣除所需神石
    player:cost_currency(GamePropIds.kGamePropIdsStoneIcon, stone)

    ---! 累加当前进度
    sw.step = sw.step + 1
    if sw.step >= artifact.levelEnergyCap then
        ---! 提升当前神器
        player:set_artifact_level(swid, next_artifact.level)
    end

    ---! 通知提升成功
    local result = {}
    result.error = 0
    result.id = swid
    player:send_packet("MSGS2CArtifactLevelup", result)

    ---! 刷新神石数量
    ITEM_D:send_currency_info(player)

    ---! 刷新神器列表
    ARTIFACT_D:send_artifact_info(player)

     ---! 同步神器排名
     RANK_D:sync_rank_data(player, "artifact_rank")
end

---! 解锁神器
function ARTIFACT_D:unlock_artifact(player, swid)
    if not ARTIFACT_D:is_artifact_opend(player) then
        ---! 神器功能未解锁
        local result = {}
        result.error = 1
        player:send_packet("MSGS2CArtifactUnlock", result)
        return
    end

    local artifact = ARTIFACT_D:find_artifact(swid)
    if not artifact then
        ---! 神器配置不存在
        local result = {}
        result.error = 2
        player:send_packet("MSGS2CArtifactUnlock", result)
        return
    end

    local swlev = player:get_artifact_level(swid)
    if swlev then
        ---! 已经解锁
        local result = {}
        result.error = 3
        player:send_packet("MSGS2CArtifactUnlock", result)
        return
    end

    if artifact.unlocktype ~= 2 then
        local result = {}
        result.error = 4
        player:send_packet("MSGS2CArtifactUnlock", result)
        return
    end

    ---! 钻石解锁
    if player:get_currency(GamePropIds.kGamePropIdsDiamondsIcon) < artifact.unlockneed then
        ---! 需要仙露不足
        local result = {}
        result.error = 5
        player:send_packet("MSGS2CArtifactUnlock", result)
        return
    end

    ---! 扣除相应钻石
    player:cost_currency(GamePropIds.kGamePropIdsDiamondsIcon, artifact.unlockneed)

    ---! 解锁当前神器
    player:set_artifact_level(swid, artifact.level)

    ---! 通知解锁成功
    local result = {}
    result.error = 0
    result.id = swid
    player:send_packet("MSGS2CArtifactUnlock", result)

    ---! 刷新钻石数量
    ITEM_D:send_currency_info(player)

    ---! 刷新神器列表
    ARTIFACT_D:send_artifact_info(player)
end

---! 通过关卡解锁神器
function ARTIFACT_D:unlock_artifact_by_stage(player, stage_level)
    if not ARTIFACT_D:is_artifact_opend(player) then
        ---! 神器功能未解锁
        return
    end

    local configs = IMPORT_D:query("sw")
    if not configs then
        return
    end

    local swids = {}
    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 1 then
            break
        end

        if stage_level <= config.unlockneed then
            break
        end

        local swlev = player:get_artifact_level(config.id)
        if swlev then
            break
        end

        swids[config.id] = 1
    until true end

    if table.size(swids) > 0 then
        ---! 解锁神器等级
        for swid, swlev in pairs(swids) do
            player:set_artifact_level(swid, swlev)
        end

        ---! 刷新神器列表
        ARTIFACT_D:send_artifact_info(player)
    end
end

---! 通过天数解锁神器
function ARTIFACT_D:unlock_artifact_by_days(player, days)
    if not ARTIFACT_D:is_artifact_opend(player) then
        ---! 神器功能未解锁
        return
    end

    local configs = IMPORT_D:query("sw")
    if not configs then
        return
    end

    local swids = {}
    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 3 then
            break
        end

        if days < config.unlockneed then
            break
        end

        local swlev = player:get_artifact_level(config.id)
        if swlev then
            break
        end

        swids[config.id] = 1
    until true end

    if table.size(swids) > 0 then
        ---! 解锁神器等级
        for swid, swlev in pairs(swids) do
            player:set_artifact_level(swid, swlev)
        end

        ---! 刷新神器列表
        ARTIFACT_D:send_artifact_info(player)
    end
end

---! 开启神器功能
function ARTIFACT_D:init_artifact_info(player)
    if not ARTIFACT_D:is_artifact_opend(player) then
        return
    end

    local configs = IMPORT_D:query("sw")
    if not configs then
        return
    end

    for _, config in pairs(configs) do repeat
        if config.unlocktype ~= 4 then
            break
        end

        local swlev = player:get_artifact_level(config.id)
        if swlev then
            break
        end

        ---! 解锁神器并开放等级
        player:set_artifact_level(config.id, 1)
    until true end

    ---! 刷新神器列表
    ARTIFACT_D:send_artifact_info(player)

    ---! 开启神器副本
    ARTIFACTSTAGE_D:init_artifactstage_info(player)

    ---! 刷新神器福利
    WELFARE_D:send_tomorrow_data(player)
end

function ARTIFACT_D:send_artifact_info(player)
    local artifact = {}
    local artifact_tbl = player:query("artifact") or {}
    for id, tbl in pairs(artifact_tbl) do
        table.insert(artifact, {
            id = tonumber(id),
            level = tbl.level,
            step = tbl.step,
        })
    end

    local result = {}
    result.artifact = artifact
    player:send_packet("MSGS2CArtifactList", result)
end
